package com.wms.controller;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wms.dto.WarehousingEntryDto;
import com.wms.dto.WarehousingEntryItemDataDto;
import com.wms.pojo.PageResult;
import com.wms.pojo.Result;
import com.wms.pojo.WarehousingEntry;
import com.wms.pojo.WarehousingEntryDetails;
import com.wms.service.WarehousingEntryDetailsService;
import com.wms.service.WarehousingEntryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/warehousingentry")
public class WarehousingEntryController {

    @Autowired
    private WarehousingEntryService warehousingEntryService;
    @Autowired
    private WarehousingEntryDetailsService warehousingEntryDetailsService;

    //条件查询
    @GetMapping
    public Result queryPage(Integer page,Integer pageSize,String batchNumber){
        PageResult pageResult = warehousingEntryService.queryPage(page,pageSize,batchNumber);
        return Result.success(pageResult);
    }

    //添加
    @Transactional
    @PostMapping
    public Result saveWarehousingEntry(@RequestBody WarehousingEntryDto dto){
        Integer count = 0;
        List<WarehousingEntryItemDataDto> detailsList = dto.getDetailsList();
        if (ObjectUtil.isNotEmpty(detailsList)){
            for (WarehousingEntryItemDataDto dataDto : detailsList) {
                count += dataDto.getCount();
            }
        }
        WarehousingEntry warehousingEntry = WarehousingEntry.builder()
                .warehouseId(dto.getWarehouseId())
                .batchNumber(dto.getBatchNumber())
                .consignorId(dto.getConsignorId())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .predictedArrivalTime(dto.getPredictedArrivalTime())
                .totalCount(count)
                .build();
        warehousingEntryService.save(warehousingEntry);

        List<WarehousingEntryDetails> list = new ArrayList<>();
        detailsList.forEach(item->{
            WarehousingEntryDetails warehousingEntryDetails = WarehousingEntryDetails.builder().warehousingEntryId(warehousingEntry.getId())
                    .price(item.getPrice())
                    .count(item.getCount())
                    .skuId(item.getId())
                    .supplierId(item.getSupplierId())
                    .predictedArrivalTime(warehousingEntry.getPredictedArrivalTime())
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build();
            list.add(warehousingEntryDetails);
        });
        warehousingEntryDetailsService.saveBatch(list);
        return Result.success();
    }

    //回显
    @GetMapping("/{id}")
    public Result getWarehousingEntryById(@PathVariable Integer id){
        WarehousingEntryDto warehousingEntryDto = warehousingEntryService.getWarehousingEntryById(id);
        return Result.success(warehousingEntryDto);
    }

    //修改数据
    @PutMapping
    @Transactional
    public Result updateById(@RequestBody WarehousingEntryDto dto){
        warehousingEntryDetailsService.remove(Wrappers.<WarehousingEntryDetails>lambdaQuery()
                .eq(WarehousingEntryDetails::getWarehousingEntryId,dto.getId()));
        Integer count = 0;
        List<WarehousingEntryItemDataDto> detailsList = dto.getDetailsList();
        if (ObjectUtil.isNotEmpty(detailsList)){
            for (WarehousingEntryItemDataDto dataDto : detailsList) {
                count += dataDto.getCount();
            }
        }
        WarehousingEntry warehousingEntry = WarehousingEntry.builder()
                .id(dto.getId())
                .warehouseId(dto.getWarehouseId())
                .batchNumber(dto.getBatchNumber())
                .consignorId(dto.getConsignorId())
                .updateTime(LocalDateTime.now())
                .predictedArrivalTime(dto.getPredictedArrivalTime())
                .totalCount(count)
                .build();
        warehousingEntryService.updateById(warehousingEntry);

        List<WarehousingEntryDetails> list = new ArrayList<>();
        detailsList.forEach(item->{
            WarehousingEntryDetails warehousingEntryDetails = WarehousingEntryDetails.builder().warehousingEntryId(warehousingEntry.getId())
                    .price(item.getPrice())
                    .count(item.getCount())
                    .skuId(item.getId())
                    .supplierId(item.getSupplierId())
                    .predictedArrivalTime(warehousingEntry.getPredictedArrivalTime())
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build();
            list.add(warehousingEntryDetails);
        });
        warehousingEntryDetailsService.saveBatch(list);
        return Result.success();
    }

    //删除
    @DeleteMapping("/{id}")
    @Transactional
    public Result deleteById(@PathVariable Integer id){
        warehousingEntryDetailsService.remove(Wrappers.<WarehousingEntryDetails>lambdaQuery()
                .eq(WarehousingEntryDetails::getWarehousingEntryId,id));
        warehousingEntryService.removeById(id);
        return Result.success();
    }
}